define(['jquery', 'bootstrap', 'userend', 'table', 'form', 'editable'], function ($, undefined, Userend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            var params = '/goods_id/' + Config.goods_id + '/cat_ids/' + Config.cat_ids;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/goods/goodsattr/index' + params,
                    add_url: 'mall/goods/goodsattr/add' + params,
                    edit_url: 'mall/goods/goodsattr/edit' + params,
                    del_url: 'mall/goods/goodsattr/del' + params,
                    multi_url: 'mall/goods/goodsattr/multi' + params,
                    table: 'mall_goods_goodsattr',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'goods_id', title: __('Goods_id'), operate: "in", formatter: Table.api.formatter.search, visible:false},
                        {field: 'attr_id', title: __('Attr_id'), visible:false},
                        {field: 'goods_id_text', title: __('Goods_id')},
                        {field: 'attr_id_text', title: __('Attr_id')},
                        {field: 'id', title: __('ID')},
                        {field: 'attrval', title: __('Attrval'), editable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter:function (value, row, index) {
                                if(row.id){
                                    return Table.api.formatter.operate.call(this, value, row, index);
                                }else{
                                    return [];
                                }
                            }
                        }
                    ]
                ],
                pagination: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $("#c-attr_id").data("params", function (obj) {
                    return {custom: {cat_id: $("#attr_ids").val()}};
                });
            }
        }
    };
    return Controller;
});